package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.GoodsCount;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("purchaseListGoods")
@Slf4j
public class PurchaseListGoodsController {

    @Autowired
    private PurchaseListGoodsService purchaseListGoodsService;
    /**
     * 进货单保存
     * @param purchaseList
     * @param purchaseListGoodsStr
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr){

       return  purchaseListGoodsService.save(purchaseList,purchaseListGoodsStr);
    }

    /**
     * 进货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime){
        HashMap<String, Object> map = new HashMap<>();
        List<PurchaseList> list =  purchaseListGoodsService.list(purchaseNumber,supplierId,state,sTime,eTime);
        map.put("rows",list);
        return map;
    }


    /**
     * 进货商品展示
     * @param purchaseListId
     * @return
     */
    @PostMapping("/goodsList")
    public Map<String,Object> goodsList(Integer purchaseListId){
        HashMap<String, Object> map = new HashMap<>();
        List<PurchaseListGoods> list = purchaseListGoodsService.goodsList(purchaseListId);
        map.put("rows",list);
        return map;
    }

    /**
     * 删除进货单
     * @param purchaseListId
     * @return
     */
    @PostMapping("/delete")
    public ServiceVO delete(Integer purchaseListId){
        return purchaseListGoodsService.delete(purchaseListId);
    }

    /**
     * 支付结算（修改进货单付款状态）
     * @param purchaseListId
     * @return
     */

    @PostMapping("/updateState")
    public ServiceVO updateState(Integer purchaseListId){
        purchaseListGoodsService.updateState(purchaseListId);
        return new ServiceVO(100,"请求成功");
    }


    /**
     * 进货统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     */
    @PostMapping("count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        List<GoodsCount> list =  purchaseListGoodsService.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String res = gson.toJson(list);
        log.info("---------进货统计--------");
        log.info(res);
        return res;
    }
}
